package com.nuoniu.sibanyun.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysArea;
import com.nuoniu.sibanyun.entity.dto.SysAreaDto;
import com.nuoniu.sibanyun.service.ISysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 行政区划 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/system/area")
@Api(value="行政区划 ",tags={"行政区划 "})
public class SysAreaController extends BaseController {

    @Autowired
    private ISysAreaService areaService;

    /**
     * 获取地区列表
     * @author 小啊刚
     * @date 2020/6/4
     */
    @GetMapping("/page")
    public HttpResult areaPage(Page page, SysArea sysArea) {
        return HttpResult.ok(areaService.areaPage(page, sysArea));
    }

    /**
     * 获取所有地区
     * @author 小啊刚
     * @date 2020-08-26
     */
    @GetMapping("treeList")
    @ApiOperation(value = "获取所有地区" ,tags = "APP_地区")
    public HttpResult treeList(Integer companyId) {
        return HttpResult.ok((areaService.treeList(companyId)));
    }

    /**
     * 保存地区
     * @author 小啊刚
     * @date 2020/6/6
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存地区" )
    public HttpResult saveArea(@RequestBody SysArea area) {
        if(StringUtils.isBlank(area.getAreaName())) {
            throw new NuoNiuException("地区名称不能为空");
        }
        if(area.getAreaCode() == null) {
            throw new NuoNiuException("地区编码不能为空");
        }

        areaService.saveArea(area);
        return HttpResult.ok();
    }

    /**
     * 删除地区
     * @author 小啊刚
     * @date 2020/6/8
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除地区" )
    public HttpResult deleteArea(Long id, Integer parentCode) {
        areaService.deleteArea(id, parentCode);
        return HttpResult.ok();
    }

    /**
     * 批量保存地区
     * @author 小啊刚
     * @date 2020/6/9
     */
    @PostMapping("/saveAreaList")
    @ApiOperation(value = "批量保存地区" )
    public HttpResult saveAreaList(@RequestBody SysAreaDto sysAreaDto) {
        if(sysAreaDto.getAreaCodes() != null && sysAreaDto.getAreaCodes().size() > 0) {
            areaService.saveAreaList(sysAreaDto);
            return HttpResult.ok();
        }
        throw new NuoNiuException("请选择地区数据");
    }

}

